package com.jyw.dialog;

import android.app.DialogFragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jyw.R;
import com.jyw.utils.DimenUtils;
import com.jyw.utils.ScreenUtils;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/3
 */

public class KefuPhoneDialog extends DialogFragment implements View.OnClickListener {
    private View mContentView;
    private TextView tvCancel, tvOk;

    public static KefuPhoneDialog newInstance() {
        return new KefuPhoneDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(false);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.dialog_kefu_phone, null);
        mContentView.setMinimumWidth(ScreenUtils.getScreenWidth(getActivity())
                - 2 * DimenUtils.dip2px(getActivity(), 72));
        return mContentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvCancel = (TextView) mContentView.findViewById(R.id.tv_cancel);
        tvOk = (TextView) mContentView.findViewById(R.id.tv_ok);
        tvCancel.setOnClickListener(this);
        tvOk.setOnClickListener(this);
    }

    /**
     * 调用拨号界面
     *
     * @param phone 电话号码
     */
    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                call("0377-63562222");
                dismiss();
                break;
        }
    }
}